package cn.nyse.ktc_sms.listenner;

import cn.nyse.ktc_sms.util.SmsUtil;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RabbitListener(queues = "sms")
public class SmsConsumer {

    @Autowired
    SmsUtil smsUtil;

    @Value("${aliyun.sms.template_code}")
    String templateCode;

    @Value("${aliyun.sms.sign_name}")
    String signName;

    @RabbitHandler
    public void sendSms(Map<String,String> map){
        System.out.println(map);
        String mobile = map.get("mobile");
        String code = map.get("code");
        try {
            SendSmsResponse smsResponse = smsUtil.sendSms(mobile, templateCode, signName, "{\"code\":\"" + code + "\"}");
            System.out.println(smsResponse.getCode());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
